package com.jxbai.design.pattern.dcxy.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;

/**
 * @author jxbai
 * @date 2022/1/12 0012
 */
public class CglibProxy {

    public static <T> T createProxy(T t) {
        //1、创建一个增强器
        Enhancer enhancer = new Enhancer();
        //2、设置要增强哪个个类的功能。增强器为这个类动态创建一个子类
        enhancer.setSuperclass(t.getClass());
        //3、设置回调
        enhancer.setCallback((MethodInterceptor) (o, method, args, methodProxy) -> {
            //编写拦截的逻辑
            System.out.println("cglib上场le .......xxx");
            //目标方法进行执行
            Object invoke = methodProxy.invokeSuper(o,args);
            return invoke;
        });
        Object o = enhancer.create();
        return (T) o;
    }
}
